go to previous page   go to home page   go to next page

Answer:

The blanks are filled in below.

(Each button must have a unique command, but you could pick different commands than the ones I chose.)


Finish actionPerformed()

We need to add some logic so that a different action is performed for each button.

When a button is clicked, it sends an ActionEvent object containing the button's command to an event listener. Use the getActionCommand() method to get the command. For example, if evt refers to an ActionEvent, then evt.getActionCommand() returns the command.

The command is a string, so use equals( String ) to compare commands.

public class TwoButtons extends JFrame implements ActionListener
{
  JButton redButton ;
  JButton grnButton ;

  // constructor for TwoButtons
  public TwoButtons()                           
  {
    . . . . . . . .
    
     redButton.setActionCommand( "red" );    
     grnButton.setActionCommand( "green" );    

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }


  public void actionPerformed( ActionEvent evt)
  {
    if ( evt.getActionCommand().equals(  ) )
    
      getContentPane().setBackground(  );    
      
    else 
    
      getContentPane().setBackground( Color.green );    

    repaint(); 
  }
  
  . . . . . . . .
  
}

QUESTION 12:

Fill in the blanks in the program.